<?php
/**
 * Created by PhpStorm.
 * User: 雨寒
 * Date: 2018/8/26
 * Time: 下午2:01
 */

namespace app\common\model;

use think\Cache;
use think\Db;
use think\exception\DbException;

class Club extends Common
{

    /*根据id 获取商会信息*/
    static public function bussinessInfoById($bussiness_id)
    {
        $info=Db::name('bussiness')
            ->where('id',$bussiness_id)
            ->where('is_valid',1)
            ->field('pro,city,title,avatar,detail,tel,is_recommend')
            ->find();
//        if(empty($bussiness_id)||empty($info))
//        {
//            throw new DeveloperException("您还没有所属商会！");
//        }
        return empty($info) ? array() : $info;
    }

    /*根据用户部门id 获取部门信息*/
    static public function departmentInfoById($department_id)
    {
        $info=Db::name('department')
            ->where('id',$department_id)
            ->field('id,bussiness_id,title,parent_id')
            ->find();
        return empty($info) ? array() : $info;
    }

    /*根据用户职务id 获取职务信息*/
    static public function dutiesInfoById($duties_id)
    {
        $info=Db::name('duties')
            ->where('id',$duties_id)
            ->field('id,title')
            ->find();
        return empty($info) ? array() : $info;
    }

    /**
     * 商会列表
     *
     * @param string $type 类型（1：轮播，2：全部）
     * @param string $page 第几页
     * @param string $pagesize 每页几个
     * @param array $user 用户
     * @return array
     */

    static public function bussinessList($type,$page,$pagesize,$user)
    {
        if($type==1)
        {
            $list=Db::name('bussiness')
                ->where('is_valid',1)
                ->where('is_recommend',1)
                ->order('display_order desc')
                ->page($page,$pagesize)
                ->select();
            if(!empty($list))
            {
                foreach ($list as &$value)
                {
                    $member=Db::name('user')
                        ->where('bussiness_id',$value['id'])
                        ->where('in_blacklist',0)
                        ->order('update_time desc')
                        ->field('id,avatar')
                        ->limit(5)
                        ->select();
                    $value['member']=$member;
                }
            }

        }else{
            $list=Db::name('bussiness')
                ->where('is_valid',1)
                ->order('display_order desc')
                ->page($page,$pagesize)
                ->select();

            if(!empty($list))
            {
                foreach ($list as &$item)
                {
                    /*加入是否关注*/
                    $follow=Db::name('user_follow')
                        ->where('uid',$user['id'])
                        ->where('follow_id',$item['id'])
                        ->where('follow_type',2)
                        ->field('id')
                        ->find();
                    if(!empty($follow))
                    {
                        $item['is_follow']=1;
                    }else{
                        $item['is_follow']=0;
                    }
                    /*加入商会人数*/
                   $member_num = Db::name('user')
                        ->where('in_blacklist',0)
                        ->where('bussiness_id',$item['id'])
                        ->count();
                    $item['member_num']=$member_num;
                    /*加入会长*/
                    $head=Db::name('user')
                        ->alias('u')
                        ->join('__DUTIES__ d',"u.duties_id=d.id")
                        ->where('u.bussiness_id',$item['id'])
                        ->where('u.duties_id',1)
                        ->field('u.id,u.mobile,u.real_name')
                        ->find();
                    if(empty($head))
                    {
                        $item['head']="暂无";
                    }else{
                        $item['head']=$head['real_name'];
                    }

                }
            }
        }

        return empty($list) ? array() : $list;

    }

    /**
     * 商会详情
     *
     * @param $bussiness_id
     * @return array
     * @internal param array $user 用户
     */

    static public function bussinessInfo($bussiness_id)
    {
        $info=Db::name('bussiness')
            ->where('id',$bussiness_id)
            ->where('is_valid',1)
            ->find();
//        if(empty($bussiness_id)||empty($info))
//        {
//            throw new DeveloperException("您还没有所属商会！");
//        }
        /*将会员信息加入商户信息中*/
        if(!empty($info))
        {
            /*会员列表*/
            $member_arr=self::bussinessMemberList(2,1,5,$bussiness_id);
            /*领导列表*/
            $leader_arr=self::bussinessMemberList(1,1,6,$bussiness_id);
            /*总人数*/
            $all_num=Db::name('user')
                ->where('bussiness_id',$bussiness_id)
                ->where('in_blacklist',0)
                ->count();
            /*新增人数*/
            $today=strtotime(date("Y-m-d"));
            $new_num=Db::name('user')
                ->where('bussiness_id',$bussiness_id)
                ->where('in_blacklist',0)
                ->where('create_time','egt',$today)
                ->count();
            /*关注数量*/
            $follow_num=Db::name('user_follow')
                ->where('follow_type',2)
                ->where('follow_id',$bussiness_id)
                ->count();

            $info['member_arr']=$member_arr;
            $info['leader_arr']=empty($leader_arr) ? array() : $leader_arr;
            $info['all_num']=$all_num;
            $info['new_num']=$new_num;
            $info['follow_num']=$follow_num;



        }
        return empty($info) ? array() : $info;

    }

    /**
     * 商会会员列表
     *
     * @param string $type 类型（1：领导，2：会员）
     * @param string $page 第几页
     * @param string $pagesize 每页几个
     * @return array
     * @internal param string $bussiness_id 商会id
     */

    static public function bussinessMemberList($type,$page,$pagesize,$bussiness_id)
    {
        if($type==1)
        {
            $list=Db::name('user')
                ->alias('u')
                ->join('__DUTIES__ d','u.duties_id=d.id','left')
                ->where('u.bussiness_id',$bussiness_id)
                ->where('u.in_blacklist',0)
                ->where('u.duties_id','neq',2)
                ->order('id')
                ->page($page,$pagesize)
                ->field('u.id as id,u.create_time as create_time,u.duties_id as duties_id,u.mobile as mobile,u.real_name,u.avatar,d.title as title')
                ->select();

        }else{
            $list=Db::name('user')
                ->alias('u')
                ->join('__DEPARTMENT__ d','u.department_id=d.id','left')
                ->where('u.bussiness_id',$bussiness_id)
                ->where('u.in_blacklist',0)
                ->where('u.duties_id',2)
                ->page($page,$pagesize)
                ->field('u.id as id,u.create_time as create_time,u.department_id as department_id,u.mobile as mobile,u.real_name,u.avatar,d.title as title')
                ->select();
        }

        return empty($list) ? array() : $list;

    }

    /**
     * 商会领导列表
     *
     * @return array
     * @internal param string $bussiness_id 商会id
     */

    static public function leaderList($bussiness_id)
    {
        $leader=Db::name('duties')
            ->where('id','neq',2)
            ->field('id,title,level')
            ->select();
        foreach ($leader as $key=>$value)
        {
            $member=Db::name('user')
                ->where('bussiness_id',$bussiness_id)
                ->where('in_blacklist',0)
                ->where('duties_id',$leader[$key]['id'])
                ->field('id,avatar,real_name,mobile,create_time')
                ->select();

            if(!empty($member))
            {
                foreach ($member as $key1=>$value1)
                {
                    $leader[$key]['member'][$key1]=$value1;
                }

            }else{
                $leader[$key]['member']=array();
            }


        }

        return empty($leader) ? array() : $leader;

    }
    /**
     * 推荐人脉列表
     *
     * @internal param array $user 用户
     * @internal param string $page 第几页
     * @internal param string $pagesize 每页数量
     *
     * @return array
     *
     */

    static public function recommendConnection($user,$page,$pagesize)
    {
        $subQuery = Db::name('user_follow')
            ->where('uid',1)
            ->column('follow_id');

        $list=Db::name('user')
            ->alias('u')
            ->join('__DEPARTMENT__ d','u.department_id=d.id','left')
            ->where('u.industry_id',$user['industry_id'])
            ->where('u.id','neq',$user['id'])
            ->where('u.in_blacklist',0)
            ->where('u.bussiness_id','gt',0)
            ->where('u.id','not in',$subQuery)
            ->order('rand()')
            ->page($page,$pagesize)
            ->field('u.id as id,u.department_id as department_id,u.mobile as mobile,u.real_name,u.avatar,d.title as title')
            ->select();

//        foreach ($list as &$item)
//        {
//            /*加入是否关注*/
//            $follow=Db::name('user_follow')
//                ->where('uid',$user['id'])
//                ->where('follow_id',$item['id'])
//                ->where('follow_type',1)
//                ->field('id')
//                ->find();
//            if(!empty($follow))
//            {
//                $item['is_follow']=1;
//            }else{
//                $item['is_follow']=0;
//            }
//        }


        return empty($list) ? array() : $list;

    }

    /**
     * 我推荐的人列表
     *
     * @internal param array $user 用户
     * @internal param string $page 第几页
     * @internal param string $pagesize 每页数量
     *
     * @return array
     *
     */

    static public function effectList($user,$page,$pagesize)
    {

        $list=Db::name('user')
            ->alias('u')
            ->join('__DUTIES__ d','u.duties_id=d.id','left')
            ->where('u.in_blacklist',0)
            ->where('u.parent_id',$user['id'])
            ->page($page,$pagesize)
            ->field('u.id as id,u.create_time as create_time,u.department_id as department_id,u.mobile as mobile,u.real_name,u.avatar,d.title as duties')
            ->select();

        foreach ($list as &$item)
        {
            /*隐藏手机号中间四位*/
            $item['mobile']=substr_replace($item['mobile'],'****',3,4);

        }

        return empty($list) ? array() : $list;

    }

    /**
     * 关注
     *
     * @internal param array $user 用户
     * @internal param array $follow_id 关注对方的id("多个id")
     * @internal param string $type 类型（1：关注会员，2：关注商会）
     * @return array
     * @throws DeveloperException
     */
    public static function bussinessFollow($user,$follow_id,$type)
    {
        foreach ($follow_id as $item)
        {
            /*关注自己*/
            if($user['id']==$item)
            {
                throw new DeveloperException("不可以关注自己！");
            }
            /*是否关注过*/
            $follow=Db::name('user_follow')
                ->where("follow_type",$type)
                ->where("uid",$user['id'])
                ->where("follow_id",$item)
                ->field("id")
                ->find();
            if($follow)
            {
                continue;
            }

            if($user['id']>=$item)
            {
                $tag=$item.",".$user['id'];
            }else{
                $tag=$user['id'].",".$item;
            }

            $data=array(
                "uid"=>$user['id'],
                "follow_type"=>$type,
                "follow_id"=>$item,
                "create_time"=>time(),
                "tag"=>$tag
            );
            $res=Db::name('user_follow')
                ->insert($data);
            if(!$res)
            {
                throw new DeveloperException("关注失败！");
            }
        }
    }

    /**
     * 我的人脉列表
     *
     * @internal param array $user 用户
     * @internal param string $page 第几页
     * @internal param string $pagesize 每页数量
     *
     * @return array
     *
     */

    static public function myConnection($user,$page,$pagesize)
    {
        $list=Db::name('user')
            ->alias('u')
            ->join('__USER_FOLLOW__ f','u.id=f.follow_id','right')
            ->where('f.uid',$user['id'])
            ->where('u.in_blacklist',0)
            ->where('u.id','neq',$user['id'])
            ->page($page,$pagesize)
            ->field('u.id as id,f.create_time as create_time,u.duties_id as duties_id,u.department_id as department_id,u.mobile as mobile,u.real_name,u.avatar')
            ->select();

        foreach ($list as &$item)
        {
            /*隐藏手机号中间四位*/
            $item['mobile']=substr_replace($item['mobile'],'****',3,4);
            /*加入职称*/
            $duties=Db::name('duties')
                ->where('id',$item['duties_id'])
                ->value('title');
            $item['duties']=$duties;
        }

        return empty($list) ? array() : $list;

    }

    /**
     * 合约互通
     *
     * @param string $type 类型（1：我请求的，2：别人请求我的）
     * @param array $user 用户
     * @return array
     */

    static public function agreementList($type,$user)
    {
        if($type==1)
        {
            $list=Db::name('bussiness_agreement')
                ->alias("ba")
                ->join('__BUSSINESS__ b','ba.receiver_bus_id=b.id')
                ->where('ba.sender_bus_id',$user['bussiness_id'])
                ->field('ba.*,b.title as title,b.avatar as avatar,b.tel as tel,b.id as bussiness_id')
                ->select();
        }else{
            $list=Db::name('bussiness_agreement')
                ->alias("ba")
                ->join('__BUSSINESS__ b','ba.sender_bus_id=b.id')
                ->where('ba.receiver_bus_id',$user['bussiness_id'])
                ->field('ba.*,b.title as title,b.avatar as avatar,b.tel as tel,b.id as bussiness_id')
                ->select();
        }

        return empty($list) ? array() : $list;

    }
}